package com.tbit.main.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.tbit.common.utils.TbitHttpServiceUtil;
import com.tbit.main.constant.BrandRemoteConfigConstant;
import com.tbit.main.constant.InternationalSmsUtil;
import com.tbit.main.pojo.Account;
import com.tbit.main.pojo.BrandConfigBO;
import com.tbit.main.pojo.BrandRemoteConfig;
import com.tbit.main.pojo.SmsAuthConfig;
import com.tbit.main.remote.sms.XCSmsClient;
import com.tbit.main.service.BrandRemoteConfigService;
import com.tbit.main.service.SmsAuthConfigService;
import com.tbit.main.service.SmsService;
import com.tbit.main.util.AliSms;
import com.tbit.main.util.ClzySmsAuthUtil;
import com.tbit.main.util.LogUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author yao
 * @since 2024/1/15 16:32
 */
@Service
public class SmsServiceImpl implements SmsService {

    @Autowired
    private SmsAuthConfigService smsAuthConfigService;

    @Autowired
    private BrandRemoteConfigService brandRemoteConfigService;

    @Override
    public void send(String country, String phone, Account accountBrand, String code) {
        BrandConfigBO brandConfig = brandRemoteConfigService.getConfigTbit(accountBrand.getAccountId()); // 判断是否兼容 tbit
        Integer templateId;  // 判断是否兼容 tbit
        BrandRemoteConfig brandRemoteConfig; // 判断是否兼容创蓝
        if (brandConfig != null && (templateId = brandRemoteConfigService.getTemplateIdTbit(accountBrand.getAccountId(), BrandRemoteConfigConstant.TBIT_SMS_CODE)) != null) { // 是否启用自研 api 短信服务
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code", code);
            TbitHttpServiceUtil.sendSms(
                    brandConfig.getAppId(),
                    brandConfig.getNonce(),
                    brandConfig.getAppSecret(),
                    phone,
                    jsonObject.toJSONString(),
                    templateId);
        } else if ((brandRemoteConfig = brandRemoteConfigService.getByAccountIdAndType(accountBrand.getAccountId(), BrandRemoteConfigConstant.CL_SMS)) != null) {
            String[] paramKV = brandRemoteConfig.getParamKV().split("&");
            String accountSms = paramKV[0].substring(paramKV[0].lastIndexOf("=") + 1);
            String passwordSms = paramKV[1].substring(paramKV[1].lastIndexOf("=") + 1);
            ClzySmsAuthUtil.sendSms(accountSms, passwordSms, "【" + accountBrand.getName() + "】您的验证码是:" + code + "。如非本人操作，请忽略本短信", phone);
        } else {
            /**获取短信配置发送验证码*/
            SmsAuthConfig smsAuthConfig = smsAuthConfigService.getByAccountId(accountBrand.getAccountId());

            if (country.equals("86")) {
                if (smsAuthConfig.getFrom() == 0) {
                    XCSmsClient.sendAuthAccountIdCode(phone, code, smsAuthConfig.getSmsCodeKey(), Integer.valueOf(smsAuthConfig.getSmsModelId()));
                } else if (smsAuthConfig.getFrom() == 1) {
                    /**兼容阿里云存放秘钥，用分号分割*/
                    String[] strings = smsAuthConfig.getSmsCodeKey().split(";");
                    LogUtil.info("阿里云：" + strings[0]);
                    AliSms.sendSms(strings[0], strings[1], smsAuthConfig.getRemark(), smsAuthConfig.getSmsModelId(), phone, code);
                }
            } else {
                /**国际短信兼容*/
                String sendPhone = "00" + country + phone;
                String content = "【Youqudrive Travel】 Your verification code is " + code + ". If you are not operating by yourself, please ignore this message";
                InternationalSmsUtil.send(sendPhone, content);
            }
        }
    }
}
